//
//  HomeMsgCell.m
//  FD
//
//  Created by Yasin on 2018/10/13.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  首页消息cell

#import "HomeMsgCell.h"

//model
#import "PushMsgModel.h"

@interface HomeMsgCell()
//背景view
@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UIImageView *iconIv;
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UILabel *timeLb;
@property (weak, nonatomic) IBOutlet UILabel *readStateLb; //未读写 已读写
/**2019-2-23 点击查看详情国际化适配*/
@property (weak, nonatomic) IBOutlet UILabel *lookDetailLb;

@end

@implementation HomeMsgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.selectionStyle = UITableViewCellSelectionStyleNone; //取消点击样式
    
    //bgview边框
    self.bgView.layer.borderWidth = 1.0;
    self.bgView.layer.borderColor = kMainLightGrayColor.CGColor;
    
    //2019-2-23国际化
    self.lookDetailLb.text = NSLocalizedString(L_DianJiChaKanXianQing, @"点击查看详情");
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



#pragma mark - 自定义右侧删除按钮样式
//!!!!:这个对iOS11之前的适用
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
    [super insertSubview:view atIndex:index];
    
    if ([view isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
//        view.top = 10;
//        view.height = self.height - 10;
        
        for (UIButton *btn in view.subviews) {
            
            if ([btn isKindOfClass:[UIButton class]]) {
                
                [btn setBackgroundColor:[UIColor whiteColor]];
                [btn setTitle:@"" forState:UIControlStateNormal];

//#error 2019-1-20请换图片
                UIImage *image = [UIImage imageNamed:@"redDelete"];
                [btn setImage:image forState:(UIControlStateNormal)];
                [btn setImage:image forState:UIControlStateHighlighted];
                [btn setTintColor:[UIColor redColor]];
            }
        }
    }
}

- (void)setModel:(PushMsgModel *)model {
    _model = model;

    //0.图标
    NSString *xxxx = [NSString stringWithFormat:@"%@%@",kPicDomain,model.thumbnail];
    NSURL *imgurl = [NSURL URLWithString:xxxx];
    FDLog(@"%@",imgurl);
    //没有占位图,不让有假数据
    UIImage *placeholderImg = [UIImage imageNamed:@""];//[UIImage imageNamed:@"1"];
    [self.iconIv sd_setImageWithURL:imgurl placeholderImage:placeholderImg];
    
    //2.标题
    self.nameLb.text = model.title ? : @"";
    
    //3.状态:已读未读
    if (model.isRead) {
        self.readStateLb.text = NSLocalizedString(L_Read, @"已读");
        //灰色背景 白色文字
        self.readStateLb.backgroundColor = [UIColor lightGrayColor];
    }else {
        self.readStateLb.text = NSLocalizedString(L_Unread, @"未读");
        self.readStateLb.backgroundColor = kMainRedColor;
    }

    //1.时间
    self.timeLb.text = [self timeStrFromTimeStamp:model.createTime];
}

- (NSString *)timeStrFromTimeStamp:(NSNumber *)timeStamp {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSTimeInterval interval = [timeStamp doubleValue] / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSString *timeStr =  [fmt stringFromDate:date];
    
    return timeStr;
}

@end
